package com.cy.lib.adapter;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Filter;
import android.widget.Filterable;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

/**
 * @param <T>
 * @author CY
 */
public abstract class BAdapter<T> extends BaseAdapter implements Filterable {

    public Activity mAct;
    public LayoutInflater mInflater;
    private List<T> mListData;

    public BAdapter(Activity act) {
        mAct = act;
        init(mAct);
    }

    private void init(Context context) {
        mInflater = LayoutInflater.from(context);
        mListData = new ArrayList<>();
    }

    public void addListData(T t, boolean isNotify) {
        mListData.add(t);
        if (isNotify) {
            notifyDataSetChanged();
        }
    }

    public void addListData(int location, T t, boolean isNotify) {
        mListData.add(location, t);
        if (isNotify) {
            notifyDataSetChanged();
        }
    }

    public void addListData(T t, boolean isNotify, int position) {
        mListData.add(position, t);
        if (isNotify) {
            notifyDataSetChanged();
        }
    }

    public void setListData(Collection<T> c) {
        if (mListData != null) {
            mListData.clear();
            mListData.addAll(c);
            notifyDataSetChanged();
        }
    }

    public void setListData(Iterator<T> iterator) {
        if (mListData != null) {
            mListData.clear();
            while (iterator.hasNext()) {
                mListData.add(iterator.next());
            }
            notifyDataSetChanged();
        }
    }

    public void addListData(Collection<T> c) {
        addListData(c, true);
    }

    public void addListData(Collection<T> c, boolean notify) {
        if (c != null) {
            mListData.addAll(c);
            if (notify) {
                notifyDataSetChanged();
            }
        }
    }

    public void addListData(int location, Collection<T> c) {
        addListData(location, c, true);
    }

    public void addListData(int location, Collection<T> c, boolean notify) {
        if (c != null) {
            mListData.addAll(location, c);
            if (notify) {
                notifyDataSetChanged();
            }
        }
    }


    public void clearListData() {
        mListData.clear();
        notifyDataSetChanged();
    }

    public void removeListData(int location) {
        mListData.remove(location);
        notifyDataSetChanged();
    }

    public void removeListData(T t) {
        mListData.remove(t);
        notifyDataSetChanged();
    }

    public void setChangeData(int position, T t) {
        mListData.set(position, t);
        notifyDataSetChanged();
    }

    public List<T> getData() {
        return mListData;
    }

    @Override
    public int getCount() {
        return mListData.size();
    }

    @Override
    public T getItem(int position) {
        return mListData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public abstract View getView(int position, View convertView, ViewGroup parent);


    @Override
    public Filter getFilter() {
        return null;
    }

    public static abstract class ViewHolder<T> {
        View mView;

        public ViewHolder(View view) {
            mView = view;
        }

        public void setData(int position, T t) {
        }
    }
}
